// pages/my/my.js
const appInstance = getApp();
var OpenId = appInstance.globalData.OpenId;
var Iv = '';
var EncryptedData = '';
var NickName = '';
var AvatarUrl = '';
var Number = ''

Page({
  /**
   * 页面的初始数据
   */
  data: {
    err: '',
    hasUserInfo: false,
    show: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    nickName: wx.getStorageSync('nickName'),
    avatarUrl: wx.getStorageSync('avatarUrl'),
    WatchList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // wx.showToast('onLoad')
    var hasUserInfo = wx.getStorageSync('hasUserInfo')
    this.setData({
      hasUserInfo: hasUserInfo,
    })
    this.getWatchList();
  },

  //获取手机号
  getPhoneNumber(e) {
    var that = this;
    Iv = e.detail.iv;
    EncryptedData = e.detail.encryptedData;
    that.ONnumber();
    that.Login();
  },
  //获取手机号方法
  ONnumber() {
    // var that = this;
    wx.request({
      url: appInstance.globalData.url + '/login/checkSession',
      method: "POST",
      data: {
        iv: Iv,
        data: EncryptedData,
        session_key: wx.getStorageSync('session_key')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        Number = res.data.data
      }
    })
  },
  //登录
  Login() {
    var that = this;
    wx.login({
      success(res) {
        wx.request({
          url: appInstance.globalData.url + '/login/3rdWeChat',
          method: 'POST',
          data: {
            source: 'wechat',
            open_id: appInstance.globalData.OpenId,
            name: wx.getStorageSync('nickName'),
            avatar: wx.getStorageSync('avatarUrl'),
            mobile: Number
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          dataType: 'json',
          success(res) {
            console.log(res)
            wx.setStorageSync("to_ken", res.data.data.pp_token)
            wx.setStorageSync("is_member", res.data.data.is_member)
            that.setData({
              show: false,
              hasUserInfo: true
            })
            if (wx.getStorageSync("to_ken")) {
              wx.navigateTo({
                url: '../my/my',
              })
            }

            wx.navigateTo({
              url: '../my/my',
            })



          },
          fail(e) {
            this.setData({
              err: JSON.stringify(e),
            })
            wx.showToast({
              title: JSON.stringify(e),
              icon: 'cancel',
              duration: 3000,
            })
          }
        })
      }
    })
  },

  //获取手机号方法
  ONnumber() {
    // var that = this;
    wx.request({
      url: appInstance.globalData.url + '/login/checkSession',
      method: "POST",
      data: {
        iv: Iv,
        data: EncryptedData,
        session_key: wx.getStorageSync('session_key')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        // console.log(res.data.data)
        Number = res.data.data
        // wx.showToast({
        //   title: Number,
        //   duration: 3000,
        // })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // wx.showToast('onLoad')
    var hasUserInfo = wx.getStorageSync('hasUserInfo')
    this.setData({
      hasUserInfo: hasUserInfo,
    })
    this.getWatchList();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getWatchList()
  },


  //获取用户信息授权登录

  onGotUserInfo(e) {
    console.log(e)
    wx.setStorageSync("nickName", e.detail.userInfo.nickName)
    wx.setStorageSync("avatarUrl", e.detail.userInfo.avatarUrl)
    wx.setStorageSync("hasUserInfo", true)
    this.setData({
      // hasUserInfo: true,
      show: true
    })
  },
  //跳转到观看历史
  watchTap() {
    wx.navigateTo({
      url: './watch_history/watch_history',
    })
  },
  //跳转到我的收藏
  my_collect() {
    wx.navigateTo({
      url: './my_favorite/my_favorite',
    })
  },

  onClose() {
    this.setData({
      show: false
    });
  },
  //观看历史列表
  getWatchList() {
    var that = this;
    wx.request({
      url: appInstance.globalData.url + '/user/listHistory',
      method: "POST",
      data: {
        pp_token: wx.getStorageSync('to_ken')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        console.log(res.data.data.today_videos)
        var VideoList = res.data.data.today_videos;
        //取数组的前两项
        var newList = VideoList.slice(0, 2)
        // console.log(newList)
        that.setData({
          WatchList: newList
        })
      }
    })

  },

  //进入详情页
  videtDetail(e) {
    console.log(e.currentTarget.id)
    var id = e.currentTarget.id
    wx.navigateTo({
      url: '../index/classify/videoDetail/videoDetail?id=' + id
    })

  },
  //跳转到设置
  setTap() {
    wx.navigateTo({
      url: '../set/set'
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    // 显示顶部刷新图标
    // wx.showNavigationBarLoading()
    // this.getWatchList()
    // wx.stopPullDownRefresh()
  }

})